import store from '../store/index'
const defaultImg = 'https://static.shuoshijiankang.com/images/theme_green/doctorHead.png'
// 将query对象转化为string
export function queryString(query) {
  let qr = ''
  for (let key in query) {
    if (query[key]) {
      qr += `&${key}=${encodeURIComponent(query[key])}`
    }
  }
  return qr.slice(1)
}
export function getMessageTime(date) {
  if (isToday(date)) {
    return wx.dayjs(date).format('HH:mm')
  }
  return wx.dayjs(date).format('YYYY-MM-DD')
}
export function formatTime(date) {
  let time = wx.dayjs(date).format('HH:mm')
  if (isToday(date)) {
    time = wx.dayjs(date).format('HH:mm')
  } else if (isYear(date)) {
    time = getDate(date)
  } else {
    time = wx.dayjs(date).format('YYYY年MM月DD日 HH:mm')
  }
  return time
}

// 日期显示MM月DD日 HH:mm
export function getDate(date) {
  return wx.dayjs(date).format('MM月DD日 HH:mm')
}

// 判断是否是当天
export function isToday(date) {
  return date.toDateString() === new Date().toDateString()
}
export function isYear(date) {
  return wx.dayjs(date).format('YYYY') === wx.dayjs(new Date).format('YYYY')
}
export function throttle(func, wait) {
  let timeout
  return function () {
    let that = this
    let args = arguments

    if (!timeout) {
      timeout = setTimeout(function () {
        timeout = null
        func.apply(that, args)
      }, wait)
    }
  }
}
// 判断是否是json string
export function isJSON(str) {
  if (typeof str === 'string') {
    try {
      let obj = JSON.parse(str)
      return !!(typeof obj === 'object' && obj)
    } catch (e) {
      return false
    }
  }
}
// 查询用户头像，返回Object{userID: avatar}
export async function getUserProfile(userIDList) {
  const avatarList = {}
  if (store.getters.isSdkReady) {
    const res = await uni.$TUIKit.getUserProfile({ userIDList })
    store.commit('updateSelectedMember', res.data)
    if (res.code === 0) {
      const data = res.data || []
      for (let i = 0; i < data.length; i++) {
        data[i].avatar = data[i].avatar || defaultImg
        avatarList[data[i].userID] = data[i]
      }
    }
  }
  return avatarList
}
// 历史消息记录
export function getMessageTimeList(list) {
  for (let i = 0; i < list.length; i++) {
    let message = list[i]
    message.isRevoked = message.recallFlag
    let nowTime = wx.dayjs().format('YYYY-MM-DD')
    let time = wx.dayjs(message.time).format('YYYY-MM-DD')
    let year1 = wx.dayjs().year()
    let year2 = wx.dayjs(message.time).year()
    let startTime = wx.dayjs(message.time).valueOf()
    let endtime = ''
    if (list[i - 1]) {
      endtime = wx.dayjs(list[i - 1].time).valueOf()
    }
    if (Math.abs(startTime - endtime) < (5 * 60 * 1000)) {
      list[i].newtime = ''
    } else {
      if (nowTime === time) {
        list[i].newtime = wx.dayjs(message.time).format('HH:mm')
      } else if (year1 === year2) {
        list[i].newtime = wx.dayjs(message.time).format('MM月DD日 HH:mm')
      } else {
        list[i].newtime = wx.dayjs(message.time).format('YYYY年MM月DD日 HH:mm')
      }
    }
  }
  return list
}